// pages/home/home.js
const {
  ScanQRcode,
  Qrcodelength,
  tagOperation
} = require('../../common/js/utils.js')
Page({

  data: {
    bannerList:'',//banner列表
    memberGrade:0,
    vitalityValue:0,
    userinfo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  onShow() {
    let that = this
    that.setData({ logincode: wx.getStorageSync('logincode') })
    // login.code 为501授权成功 502 未授权 不用调用用户信息接口
    console.log(wx.getStorageSync('logincode'));
    if (wx.getStorageSync('logincode') == 501) {
      //  获取用户信息
      that.api.interface.getCustomerInfo().then(res => {
        wx.setStorageSync('userinfo', res.data.data)
        wx.setStorageSync('customerId', res.data.data.customerId)
        that.setData({
          userinfo: (res.data.data || {}).hasOwnProperty('nickName') ? res.data.data : { nickName: '' }
        })
      })
    }
    // 获取首页信息
    that.api.interface.openinginformation().then(res => {
      wx.setStorageSync('time', res.data.data.dateNow)
      wx.setStorageSync('openinginformation', res.data.data);
      if(res.data.data.customerHomeInfoResponse){
        that.setData({
          memberGrade: res.data.data.customerHomeInfoResponse.memberGrade,
          vitalityValue:res.data.data.customerHomeInfoResponse.vitalityValue,
          bannerList: res.data.data.bannerList,
        })
      }
      that.setData({
        bannerList: res.data.data.bannerList,
      })
    })
  },
  cli_exchange(){
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+1});
      return false;
    }

    wx.navigateTo({
      url: '../store/store',
    });
  },
  cli_auth(){
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+1});
    }
  },
  cli_staff(){
    wx.navigateTo({
      url: '../staff/staff',
    });
  },
  openScan() {
    let that = this;
    var userinfo = wx.getStorageSync('userinfo');
    if(!userinfo){
      wx.navigateTo({ url:'../authorization/authorization?act='+1});
      return false;
    }
    wx.requestSubscribeMessage({
      tmplIds: ['vUCAXpjImMVfwxPDJ-_0GUQsZut9nIWp5nA388ppsNc'],
      success:res=>{
        that.api.interface.userSubscribeMessage({type: 4});
      },
      complete() {
        wx.scanCode({
          onlyFromCamera: true,
          success(res) {
            console.log('---d----- -- ->', res)
            if (!res.result) {
              wx.navigateTo({
                url: '/pages/isnotcorrect/isnotcorrect',
              })
              return false;
            }
            var qrcode = res.result;
            var code;
            if (38 == qrcode.length) {
              code = qrcode.substring(26);
            } else if (43 == qrcode.length) {
              code = qrcode.substring(27);
            }
            that.setData({
              code: code
            });
            that.onSubmit(that);
          }
        });

      }
    });
  },
  onSubmit(that) {
    // 请求接口提交
    this.api.interface.smallScanPoint({
      pinCode: that.data.code
    }).then(result => {
      ScanQRcode(result)
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})